
var layer = layui.layer //弹层
    ,table = layui.table //表格
    ,form = layui.form,$ = layui.jquery,laydate = layui.laydate;
lay('.starttime').each(function(){
    laydate.render({
        elem: this
    });
});
layer.ready(function () {
    //获取所有用户信息
    $.ajax({
        url:"otherOutStock/getAllUserInfo",
        success:function(data){
            if(data.status == 200){
                for(let i in data.data){
                    $("#creatorNo").append("<option value=\"" + data.data[i].code + "\">" + data.data[i].name + "</option>");
                }
                form.render('select');
                return false;
            }
        }
    });
})
form.render();//表单刷新
table.render({
    elem: '#inventoryselect',
    url: 'inventory/getListByPage',
    toolbar: '#inventorytoolbar',
    cols: [[
        {checkbox: true, fixed: true}
        ,{field:'billNo', title:'盘点单号',sort:true,event:'billNo'}
        ,{field:'operationDate', title:'盘点时间',sort:true}
        ,{field:'manager', title:'经手人',sort:true}
        ,{field:'creator', title:'创建人',sort:true}
        ,{field:'createDate', title:'创建时间',sort:true}
        ,{field:'memo', title:'备注'}
        ,{fixed: 'right', width:240, toolbar: '#inventoryeditor', title:'操作'}
    ]]
    ,id: 'inventorytableid'
    ,height: $(document).height() - $('#inventoryselect').offset().top - 50
    ,page: true,
    parseData: function(res){
        return {
            "code": res.status,
            "count": res.total,
            "data": res.data
        };
    },
});
table.on('tool(inventorytable)', function(obj){
    let data = obj.data;
    let id = data.id;
    if(obj.event === 'update') {
        layer.msg('修改');
        parent.intab("views/jsp/WarehouseManagement/tpl/inventoryUpdate.jsp?id="+id,"修改盘点")
    }else if(obj.event === 'delect'){
        layer.confirm("确认删除？",
            confirmInfo(3,6),
            function (index) {
                layer.close(index);
                let dataBases={
                    id:id,
                    status:data.status
                }
                $.ajax({
                    type:"POST",
                    dataType:"json",
                    contentType: 'application/json',
                    data:JSON.stringify(dataBases),
                    url:"inventory/delete",
                    success:function(res){
                        if(res.status == 200){
                            layer.msg("删除成功", {icon: 6});
                            table.reload('inventorytableid', {
                                page: {
                                    limit:10,
                                    curr: 1 //重新从第 1 页开始
                                }
                                ,where: data.field
                            }, 'data');
                            return false;
                        }else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    }
                });
            });
    }else if(obj.event === 'submits'){
        layer.confirm('确定提交审批吗?', confirmInfo(3, 5), function (index) {
            operationData(id, 1, "提交审批");
        });
    }else if(obj.event === 'through'){
        layer.confirm('确定审批通过吗?', confirmInfo(3, 5), function (index) {
            operationData(id, 2, "审批通过");
        });
    }else if(obj.event === 'backtothe'){
        layer.confirm('确定审批退回吗?', confirmInfo(3, 5), function (index) {
            operationData(id, 3, "审批退回");
        });
    }else if(obj.event === 'complete'){
        layer.confirm('确定完成吗?', confirmInfo(3, 5), function (index) {
            operationData(id, 4, "完成");
        });
    }else if(obj.event === 'reduction'){
        layer.confirm('确定还原吗?', confirmInfo(3, 5), function (index) {
            operationData(id, 5, "还原");
        });
    }else if(obj.event === 'billNo'){
        parent.intab("views/jsp/WarehouseManagement/tpl/viewinventory.jsp?id="+obj.data.id,"盘点明细")
    }
});


function operationData(id, operation, msg){
    let data = {
        id : id,
        operation: operation
    }
    $.ajax({
        type:"POST",
        dataType:"json",
        contentType: 'application/json',
        data:JSON.stringify(data),
        url:"inventory/operation",
        success:function(res){
            if(res.status == 200){
                layer.msg(msg, {icon: 6});
                table.reload('inventorytableid', {
                    page: {
                        limit:10,
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: data.field
                }, 'data');
                return false;
            }else {
                layer.msg(res.msg, {icon: 5});
            }
        }
    });
}


form.on('submit(submitform)', function(data){
    table.reload('inventorytableid', {
        page: {
            limit:10,
            curr: 1 //重新从第 1 页开始
        }
        ,where: data.field
    }, 'data');
    return false;
});

table.on('toolbar(inventorytable)', function(obj){
    switch(obj.event){
        case 'news':
            parent.intab("views/jsp/WarehouseManagement/tpl/newupinventory.jsp","新增盘点")
            break;
    };
});


$("#statistical").on("click",function () {
    parent.intab("views/jsp/WarehouseManagement/tpl/statistics.jsp","盘点统计")
})

//提示信息渲染
function confirmInfo(icon, anim) {
    let data = {
        icon: icon,
        title: "提示",
        offset: "auto",
        skin: 'layui-layer-molv',
        anim: anim
    }
    return data;
}